﻿using spriteeditor.WorkingSpace;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ttbit.core;

namespace spriteeditor
{
    public class Edit_Select
    {
        Form1 form;
        public Edit_Select(Form1 form)
        {
            this.form = form;
        }
        void OnSelect_NewTexLis(Panel root, TreeNode node, NodeTag_NewTexList _list)
        {
            var btn = new Button() { Text = "转换所有为Sprite", Width = 200, Height = 32 };
            var label = new Label() { Text = "PlayerName:属于哪个角色", AutoSize = true };
            var playertext = new TextBox() { Width = 200 };
            var label2 = new Label() { Text = "Part:属于哪个部位", AutoSize = true };
            var parttext = new TextBox() { Width = 200 };
            var label3 = new Label() { Text = "给个PlayerName与Part用于分类，否则你将来都找不到图", AutoSize = true };
            root.Controls.Add(btn);
            root.Controls.Add(label);
            root.Controls.Add(playertext);
            root.Controls.Add(label2);
            root.Controls.Add(parttext);
            root.Controls.Add(label3);


            btn.MouseClick += (s, e) =>
            {
                foreach (TreeNode n in node.Nodes)
                {
                    if (n.Tag is NodeTag_NewTex _newte)
                    {
                        form.workingSpace.ConvertWildTex2Sprite(_newte.texname, playertext.Text, parttext.Text);
                    }
                }
                form.RefreshAllSprite();
            };

            //显示所有野图
            {
                form.workingSpace.usermode = 1;
                if (form.workingSpace.usermode_showlist == null)
                    form.workingSpace.usermode_showlist = new List<WorkingSpace.SkinSprite>();
                form.workingSpace.usermode_showlist.Clear();

                foreach (TreeNode n in node.Nodes)
                {
                    if (n.Tag is NodeTag_NewTex _newte)
                    {
                        var nsprite = new WorkingSpace.SkinSprite()
                        {
                            name = "",

                            filename = _newte.texname,
                            uv = new Rectangle(0, 0, -1, -1)

                        };
                        form.workingSpace.usermode_showlist.Add(nsprite); ;
                    }
                }
            }
        }

        void OnSelect_NewTex(Panel root, TreeNode node, NodeTag_NewTex _tex)
        {
            var btn = new Button() { Text = "转换为Sprite", Width = 200, Height = 32 };
            var label = new Label() { Text = "PlayerName:属于哪个角色", AutoSize = true };
            var playertext = new TextBox() { Width = 200 };
            var label2 = new Label() { Text = "Part:属于哪个部位", AutoSize = true };
            var parttext = new TextBox() { Width = 200 };
            var label3 = new Label() { Text = "给个PlayerName与Part用于分类，否则你将来都找不到图", AutoSize = true };
            root.Controls.Add(btn);
            root.Controls.Add(label);
            root.Controls.Add(playertext);
            root.Controls.Add(label2);
            root.Controls.Add(parttext);
            root.Controls.Add(label3);

            btn.MouseClick += (s, e) =>
            {
                form.workingSpace.ConvertWildTex2Sprite(_tex.texname, playertext.Text, parttext.Text);

                form.RefreshAllSprite();
            };

            //显示单个野图
            {
                form.workingSpace.usermode = 1;
                if (form.workingSpace.usermode_showlist == null)
                    form.workingSpace.usermode_showlist = new List<WorkingSpace.SkinSprite>();
                form.workingSpace.usermode_showlist.Clear();
                var nsprite = new WorkingSpace.SkinSprite()
                {
                    name = "",

                    filename = _tex.texname,
                    uv = new Rectangle(0, 0, -1, -1)

                };
                form.workingSpace.usermode_showlist.Add(nsprite); ;
            }
        }
        void FindSpriteOnNode(TreeNode node, List<SkinSprite> result)
        {
            foreach (TreeNode snode in node.Nodes)
            {
                if (snode.Tag is NodeTag_Sprite _s)
                {
                    result.Add(_s.sprite);
                }
                if (snode.Tag is NodeTag_SpriteList _list)
                {
                    FindSpriteOnNode(snode, result);
                }
            }
        }
        void OnSelect_SpriteList(Panel root, TreeNode node, NodeTag_SpriteList _spriteList)
        {
            List<SkinSprite> allsprites = new List<SkinSprite>();
            FindSpriteOnNode(node, allsprites);
            //显示多个精灵
            {
                form.workingSpace.usermode = 2;
                form.workingSpace.usermode_editsprite = null;
                if (form.workingSpace.usermode_showlist == null)
                    form.workingSpace.usermode_showlist = new List<WorkingSpace.SkinSprite>();
                form.workingSpace.usermode_showlist.Clear();

                form.workingSpace.usermode_showlist.AddRange(allsprites);
            }
        }
        void OnSelect_Sprite(Panel root, TreeNode node, NodeTag_Sprite _sprite)
        {
            var label = new Label() { Text = "所有的修改都是对临时对象的,直到你按了下面的Save", AutoSize = true };

            var btn = new Button() { Text = "撤销修改", AutoSize = true };
            btn.MouseClick += (s, e) =>
            {
                _sprite.Revert();
                form.propertyGrid1.Refresh();
            };
            var btn2 = new Button() { Text = "保存修改", AutoSize = true };
            btn2.MouseClick += (s, e) =>
            {
                _sprite.Save();
                form.workingSpace.Save();
                form.propertyGrid1.Refresh();
            };
            root.Controls.Add(label);
            root.Controls.Add(btn);
            root.Controls.Add(btn2);
            //显示单个精灵
            {
                form.workingSpace.usermode = 2;
                if (form.workingSpace.usermode_showlist == null)
                    form.workingSpace.usermode_showlist = new List<WorkingSpace.SkinSprite>();
                form.workingSpace.usermode_showlist.Clear();

                form.workingSpace.usermode_showlist.Add(_sprite.sprite);
            }
            form.workingSpace.usermode_editsprite = _sprite;

        }
        public void OnSelect(TreeNode node, INodeTag tag)
        {
            form.propertyGrid1.SelectedObject = tag;
            var root = form.flowLayoutPanel2;
            root.Controls.Clear();
            if (tag != null)
            {
                if (tag is NodeTag_NewTexList _list)
                {
                    OnSelect_NewTexLis(root, node, _list);
                }
                if (tag is NodeTag_NewTex _tex)
                {
                    OnSelect_NewTex(root, node, _tex);
                }
                if (tag is NodeTag_SpriteList _splist)
                {
                    OnSelect_SpriteList(root, node, _splist);
                }
                if (tag is NodeTag_Sprite _sprite)
                {
                    OnSelect_Sprite(root, node, _sprite);
                }
            }
        }
        public void OnPropChange(INodeTag tag)
        {
            //if (tag is NodeTag_Sprite _s)
            //{
            //    if (_s.Povit != _s.sprite.povit)
            //    {
            //        form.workingSpace.usermode_editpovit = _s.Povit;
            //    }
            //    else
            //    {
            //        form.workingSpace.usermode_editpovit = null;
            //    }
            //}
        }
        int dx = 0;
        int dy = 0;
        int px = 0;
        int py = 0;
        public void OnPointDown(INodeTag tag, int x, int y)
        {
           
            if (tag is NodeTag_Sprite _s)
            {
                dx = x;
                dy = y;
                px = _s.Povit.X;
                py = _s.Povit.Y;
            }

        }
        public void OnPointMove(INodeTag tag, int x, int y)
        {
            if (tag is NodeTag_Sprite _s)
            {
                _s.Povit = new Point((int)(px + (x - dx)/form.workingSpace.usermode_scale),(int)( py + (y - dy)/form.workingSpace.usermode_scale));
                form.propertyGrid1.Refresh();
            }
        }
        public void OnPointUp(INodeTag tag, int x, int y)
        {

        }

    }
}
